package com.lanmao.mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanmao.mall.converter.CategoryConverter;
import com.lanmao.mall.entity.Category;
import com.lanmao.mall.mapper.CategoryMapper;
import com.lanmao.mall.service.CategoryService;
import com.lanmao.mall.vo.CategoryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author lanmao
 * @since 2023-11-24
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private final CategoryConverter categoryConverter;
    @Override
    public List<CategoryVO> getCategoryList() {
        List<Category> categoryList = this.baseMapper.selectList(null);
        List<CategoryVO> categoryVOList = categoryConverter.entity2Vos(categoryList);

        Map<Long, List<CategoryVO>> collect = categoryVOList.stream().collect(Collectors.groupingBy(CategoryVO::getParentCid));

        categoryVOList.forEach(category -> {
            if (collect.containsKey(category.getCatId())) {
                category.setChildren( collect.get(category.getCatId()));
            }
        });
        return categoryVOList.stream().filter(category -> category.getParentCid() == 0).collect(Collectors.toList());
    }

}
